package pages;

import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import selenium.WebDriverWrapper;
import utils.Log4Test;
import org.openqa.selenium.By;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by Sasha on 14.07.2014.
 */
public class HomePage {
    protected String URL = "http://rozetka.com.ua/";
    //public WebDriverWrapper driver;
    public WebDriver driver;
    private static final By searchString  = By.className("header-search-input-text");
    private static final By productNew = By.className("g-i-list-title");
    private static final By addToCompare = By.name("tocomparison");
    private static final By compare = By.xpath("//a[contains(@href,'comparison')]");
    private static final String findProd = "//td[%s]//div[@class='item available']//div[@class='title']//a";

    private static final int PROD1_NUMBER = 2;
    private static final int PROD2_NUMBER = 3;

    public HomePage(WebDriver driver){
        this.driver = driver;
    }

    public void open(){
        driver.get(URL);
    }

    public boolean isOpened(){
        return driver.getCurrentUrl().equals(URL);
    }

    public String searchProduct(String productName) {
        driver.findElement(searchString).clear();
        driver.findElement(searchString).sendKeys(productName);
        driver.findElement(searchString).sendKeys(Keys.RETURN);
        String findProduct = driver.findElement(productNew).getText();
        return findProduct;
    }

    public void addToComprasion(){
        driver.findElement(addToCompare).click();
    }

    public void openCompressionPage(){
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        WebDriverWait wait = new WebDriverWait(driver,30);
        wait.until(ExpectedConditions.elementToBeClickable(compare));
        driver.findElement(compare).click();
        Log4Test.info("Сторінка порівняння відкрита");
    }

    public List<String> findProduct(){
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        List<String> list = new ArrayList<String>();
        list.add( driver.findElement(By.xpath(String.format(findProd,PROD1_NUMBER))).getText());
        list.add(driver.findElement(By.xpath(String.format(findProd,PROD2_NUMBER))).getText());
        return list;
    }
}
